<?php

namespace App\Observers;

use App\Http\Controllers\RushBuy;
use App\Models\GrCoupon;
use App\Models\GrCouponActivity;

class GrCouponActivityObserver
{
    /**
     * Handle the GrCouponActivity "created" event.
     *
     * @param \App\Models\GrCouponActivity $grCouponActivity
     * @return void
     */
    public function created(GrCouponActivity $grCouponActivity)
    {
        $activityId = $grCouponActivity->activity_id;
        $activityStock = $grCouponActivity->activity_stock;
        //取出优惠券库存   根据活动需求对库存进行减少
        $data = GrCouponActivity::where('activity_id', $activityId)->pluck('coupon_id')->toArray();
        $coupon_id = $data[0];
        $couponStock = GrCoupon::where('coupon_id', $coupon_id)->pluck('coupon_id')->toArray();
        $couponStock=$couponStock[0];
        GrCoupon::where('coupon_id', $coupon_id)->update(['coupon_stock' => $couponStock - $activityStock]);
        $activity_exit_time = $grCouponActivity->activity_exit_time;
        $activity_start_time = $grCouponActivity->activity_start_time;
        $model = new RushBuy();
        $model->rushBuy($activityId, $activityStock, $activity_exit_time, $activity_start_time);
    }

    /**
     * Handle the GrCouponActivity "updated" event.
     *
     * @param \App\Models\GrCouponActivity $grCouponActivity
     * @return void
     */
    public function updated(GrCouponActivity $grCouponActivity)
    {
        //
    }

    /**
     * Handle the GrCouponActivity "deleted" event.
     *
     * @param \App\Models\GrCouponActivity $grCouponActivity
     * @return void
     */
    public function deleted(GrCouponActivity $grCouponActivity)
    {
        //
    }

    /**
     * Handle the GrCouponActivity "restored" event.
     *
     * @param \App\Models\GrCouponActivity $grCouponActivity
     * @return void
     */
    public function restored(GrCouponActivity $grCouponActivity)
    {
        //
    }

    /**
     * Handle the GrCouponActivity "force deleted" event.
     *
     * @param \App\Models\GrCouponActivity $grCouponActivity
     * @return void
     */
    public function forceDeleted(GrCouponActivity $grCouponActivity)
    {
        //
    }
}
